
package com.probiz.estoresa.content.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.common.model.content.TemplateInfo;
import com.probiz.estore.content.service.TemplateManager;
import com.probiz.estore.core.controller.BaseController;
import com.probiz.estore.webapp.util.RequestUtil;

public class TemplateController extends BaseController {
	private TemplateManager	templateManager	= null;

	public ModelAndView defaultAction(HttpServletRequest request, HttpServletResponse response) 
	{
	    if (RequestUtil.isSelectorUri(request))
	        return selectTemplates(request, response);
	    else
	        return listTemplates(request, response);
	}

	public ModelAndView deleteTemplates(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String[] ids = request.getParameterValues("multiIds");
		if (ids != null) {
			for (int i = 0; i < ids.length; i++) {
				String templatePath = ids[i];
				templateManager.deleteTemplate(templatePath);
			}
		}

		return listTemplates(request, response);
	}


	public ModelAndView listTemplates(HttpServletRequest request,
			HttpServletResponse response) {
		String templateType = RequestUtil.getParameterDefaultSafe(request, "templateType", "jsp");

		List<TemplateInfo> templateTreeList = templateManager
				.getTemplateTreeList(templateType);
		request.setAttribute("templateType", templateType);

		return new ModelAndView("content/templateList", "templateTreeList",
				templateTreeList);
	}


	private ModelAndView selectTemplates(HttpServletRequest request,
			HttpServletResponse response) 
	{
		List<TemplateInfo> templateTreeList = templateManager
				.getTemplateTreeList(RequestUtil.getParameterDefaultSafe(request, "templateType", "jsp"));
		return new ModelAndView("content/templateSelecter", "templateTreeList",
				templateTreeList);
	}

	public void setTemplateManager(TemplateManager inMgr) {
		this.templateManager = inMgr;
	}

}
